/*
* Copyright 2017 Yonyou Auto Information Technology（Shanghai） Co., Ltd. All Rights Reserved.
*
* This software is published under the terms of the YONYOU Software
* License version 1.0, a copy of which has been included with this
* distribution in the LICENSE.txt file.
*
* @Project Name : marketing-service-wechatpush
*
* @File name : MessageHistoryService.java
*
* @Author : Administrator
*
* @Date : 2017年8月16日
*
----------------------------------------------------------------------------------
*     Date       Who       Version     Comments
* 1. 2017年8月16日    Administrator    1.0
*
*
*
*
----------------------------------------------------------------------------------
*/

package com.yonyou.gmmc.service.wechatpush.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.transaction.Transactional;

import org.apache.log4j.Logger;
import org.marketing.common.event.BizEvent;
import org.marketing.common.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yonyou.gmmc.service.wechatpush.dao.TtMessageMapper;
import com.yonyou.gmmc.service.wechatpush.model.TtMessage;

import tk.mybatis.mapper.entity.Example;

/**
 * @author Administrator
 * @date 2017年8月16日
 */
@Transactional
@Service
public class MessageHistoryService {

    private static final Logger logger = Logger.getLogger(MessageHistoryService.class);

    @Autowired
    private TtMessageMapper     ttMessageMapper;

    public List<TtMessage> MessageList(String userId) {
        List<TtMessage> result = new ArrayList<>();
        result = ttMessageMapper.selectMessageList(userId);
        logger.info("查询有" + result.size() + "条消息");
        String date = DateUtil.formateDate(new Date(), "yyyy-MM-dd");
        String dateEquals = "";
        logger.info("当前日期为："+date);
        for(int a=0;a<result.size();a++){
            logger.info("判断消息生成时间是否为当日，当天的消息不显示年月日");
            Date create = result.get(a).getCreateDate();
            String createDate = DateUtil.formateDate(create, "yyyy-MM-dd");
            String time = DateUtil.formateDate(create, "HH:mm");
            logger.info("消息的创建时间为："+createDate); 
            if(createDate.equals(date)){
                logger.info("该消息为当天生成的，不显示年月日");
                result.get(a).setDate("");
                result.get(a).setTime(time);
            }else{
                logger.info("该消息不是当日的，需要显示年月日");
                if(dateEquals.equals(createDate)){
                    result.get(a).setDate("");
                    result.get(a).setTime(time);
                }else{
                    result.get(a).setDate(createDate);
                    result.get(a).setTime(time);
                    dateEquals = createDate;
                }
            }
        }
        return result;
    }

    public List<TtMessage> Message(String id) {
        List<TtMessage> result = new ArrayList<>();
        result = ttMessageMapper.selectMessage(id);
        logger.info("查询有" + result.size() + "条消息");
        return result;
    }

    public Map<String, Object> SendMessage(BizEvent event) throws Exception {
        Map<String, Object> result = new HashMap<>();
        try {
            TtMessage record = new TtMessage();
            record.setMessageContent(event.getEventData().get("MESSAGECONTENT").toString());
            record.setMessageTitle(event.getEventData().get("MESSAGETITLE").toString());
            record.setMessageType(event.getEventData().get("MESSAGETYPE").toString());
            record.setReadingFlag("0");
            record.setUserId(Long.valueOf(event.getEventData().get("USERID").toString()));
            record.setCreateDate(new Date());
            int a = ttMessageMapper.insertSelective(record);
            logger.info("新增了" + a + "条消息");
            if (a == 1) {
                result.put("result", "200");
                result.put("resultMsg", "新增消息记录成功");
            } else {
                result.put("result", "500");
                result.put("resultMsg", "新增消息记录失败");
            }
        } catch (Exception e) {
            logger.info("Exception:", e);
            result.put("result", "500");
            result.put("resultMsg", "新增消息记录异常");
        }
        return result;
    }

    public Map<String, Object> updateMessageType(String id) {
        Map<String, Object> result = new HashMap<>();
        try {
            Example e = new Example(TtMessage.class);
            e.createCriteria().andCondition("MESSAGE_ID=", id);
            TtMessage record = new TtMessage();
            record.setReadingFlag("1");
            record.setUpdateDate(new Date());
            int a = ttMessageMapper.updateByExampleSelective(record, e);
            logger.info("更新了" + a + "条消息状态");
            if (a == 1) {
                result.put("result", "200");
                result.put("resultMsg", "更新消息状态成功");
            } else {
                result.put("result", "500");
                result.put("resultMsg", "更新消息状态失败");
            }
        } catch (Exception e) {
            logger.info("Exception:", e);
            result.put("result", "500");
            result.put("resultMsg", "更新消息状态异常");
        }
        return result;
    }

    public Integer MessageCount(String userId) {
    	TtMessage ms = new TtMessage();
    	ms.setUserId(Long.valueOf(userId));
    	ms.setReadingFlag("0");
    	Integer result = ttMessageMapper.selectCount(ms);
        logger.info("未读消息数为：" + result);
        return result;
    }
}
